import { EggAppConfig, EggAppInfo, PowerPartial } from 'egg';
import * as path from 'path';

// for config.{env}.ts
export type DefaultConfig = PowerPartial<EggAppConfig & BizConfig>;

// app special config scheme
export interface BizConfig {
    sourceUrl: string;
}

export default (appInfo: EggAppInfo) => {
    const config = {} as PowerPartial<EggAppConfig> & BizConfig;

    // app special config
    config.sourceUrl = `https://github.com/eggjs/examples/tree/master/${appInfo.name}`;

    // override config from framework / plugin
    // use for cookie sign key, should change to your own and keep security
    config.keys = appInfo.name + '_1534490368803_557';

    // add your config here
    config.middleware = ['checkToken', 'checkUserToken', 'gzip'];
    config.checkToken = {
        name: 'cbl',
    };

    config.systemDefaultConfig = {
        user: {
            defaultUsername: 'admin',
            defaultPassword: '123456',
        },
        setting: {
            userViewTimeOut: '5',
            userExcuseTime: '10',
            commentHints: '老铁。。。',
            freshUserTime: '180',
            freshUserNum: '5',
            notificationInterval: '60',
            systemInformIcon: 'kkk.png',
            anonymousIcon: 'aa.png',
            homePageImg: 'ss.png',
            registerProtocol: '不能非法使用',
            shareLink: 'https://www.baidu.com/',
            cueWord:'户外活动，注意安全',
            keyWorkImg:'aa.png',
            enrollProtocol:'报名协议'
        },
    };

    config.development = {
        watchDirs: [],
        ignoreDirs: [],
        fastReady: false,
        reloadOnDebug: true,
        overrideDefault: false,
        reloadPattern: undefined,
    };
    config.sequelize = {
        dialect: 'mysql',
        database: 'db_makefriend',
        host: 'www.blueleader.top',
        port: 3306,
        username: 'root',
        password: 'qq@303269789',
        pool: {
            max: 100,
            min: 10,
        },
        dialectOptions: {
            charset: 'utf8mb4'
        },
    };
    config.cors = {
        origin: '*',
        allowMethods: 'GET,POST,PUT,DELETE,OPTIONS',
    };
    config.multipart = {
        whitelist: [
            '.png',
            '.jpg',
            '.jpeg',
            '.gif',
        ],
        requireFile: false,
    };
    config.static = {
        prefix: '/',
        dir: path.join(appInfo.baseDir, 'app/public'),
        dynamic: true,
        preload: false,
        maxAge: 31536000
    };
    config.onerror = {
        json: (err, ctx) => {
            console.log(err.message);
            let result:any;
            try{
                result=JSON.parse(err.message);
            }catch(e){
                result=err.message;
            }
            if(result.code){
                ctx.body=result;
            }else{
                ctx.body={code:3,msg:result};
            }
            ctx.status = 200;
            // ctx.body = JSON.parse(err.message);
            // ctx.status = 200;
        },
        html: (err, ctx) => {
            console.log(err.message);
            let result:any;
            try{
                result=JSON.parse(err.message);
            }catch(e){
                result=err.message;
            }
            if(result.code){
                ctx.body=JSON.stringify(result);
            }else{
                ctx.body=JSON.stringify({code:3,msg:result});
            }
            //ctx.body = err.message;
            ctx.status = 200;
        },
    };
    config.gzip = {
        pageSize: 10,
    };
    config.cacheTick = '6m';
    config.overTime = '6m';
    config.notificationInterval = '60m';
    config.security = {
        csrf: false,
    };
    config.rongyun = {
        appSecret: 'UHoyd5Wmy4Sh',
        appKey: 'n19jmcy5953r9'
    };
    config.weixin = {
        appId: 'wxc2e4ac2e744d742a',
        appSecret: 'e72dcebd2abf05bd16d8a4a1227dab0a'
    };
    config.SMS = {
        AccessKeyID: 'LTAIiL6S23ytIo8H',
        AccessKeySecret: 'j3oLUqgqYyopeDmGOdciN3sONEqyrK'
    };
    config.baseUrl = 'http://makefriend.blueleader.top/';
    config.redirectUrl = 'http://makefriend.blueleader.top/';
    config.templateMessageId={
        noticeMessage:'oxqgxEamAJxFBi5sS0myHisYYHaIxp2sAWZcj6o5aT4',
        auditingMessage:'JnnN6dlRBIN-Vj9bH8nNjL4EFiI1xriRrvB3OsSTKuY',
        makeFriendMessage:'Pkz_JtOFBrX6MDQPi-cwlQWniq-SFowh-b2RUWT4Y3o'
    }
    return config;
};
